﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TSV.Models;
using TSV.Library.Exceptions;
using TSV.Library.Attributes;
using TSV.Library.Common;
using System.Configuration;
using System.Web.Routing;
using TSV.Areas.Administration.Models;
namespace TSV.Controllers
{
    public abstract class BaseViewController : Controller
    {        
        private string msgHoder = "<div id=\"systemmessage\" class=\"{0}\">{1}</div>";

        public BaseViewController()
          {
              ViewData["NewPartner"] = new PartnerDao().GetListNewPartner();
              ViewData["FooterMenu"] = new CategoryDao().GetList();
              //ViewData["PromotionInfomation"] = new PromotionDao().GetList();
              ViewData["ListBigBanner"] = new AdvertisingDao().GetListBigPartner();
              ViewData["GlobalCategory"] = new SelectList(new CategoryDao().GetList(),"ID","CategoryName","");
              //ViewData["AdvertisingBottom"] = new AdvertisingDao().GetAdvertisingBottom();
          }


        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                string message = "";

                Exception ex = filterContext.Exception;
                filterContext.ExceptionHandled = true;

                if (ex is CrmException)
                {
                    CrmException ge = (CrmException)ex;
                    if (ex is ForbiddenExceptionOnCurrentPage)
                    {
                        string controllerName = filterContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
                        Message msg = new Message(MessageConstants.E0002, MessageType.Error);
                        ShowMessage(msg);                        
                        this.RedirectToAction("Index", controllerName).ExecuteResult(this.ControllerContext);
                    }
                    else
                    {                        
                        message = ge.ErrorMessage;                        
                        ViewData["ErrorMessage"] = message;
                        this.View("Error").ExecuteResult(this.ControllerContext);
                    }                    
                }
                else if (ex is Exception)
                {
                    message = ex.Message;                    
                    ViewData["ErrorMessage"] = message;
                    this.View("Error").ExecuteResult(this.ControllerContext);
                }                
            }
        }
        #region helper methods
                

        //duy hung sua
        protected void ShowMessage(Message msg)
        {
            string message = string.Empty;
            if (msg.MsgType == MessageType.Error)
            {
                message = string.Format(msgHoder, "msgError", msg.MsgText);
            }
            else if (msg.MsgType == MessageType.Warning)
            {
                message = string.Format(msgHoder, "msgWarning", msg.MsgText);
            }
            else
            {
                message = string.Format(msgHoder, "msgSuccess", msg.MsgText);    
            }

            TempData["Message"] = message;
        }

        //duy hung sua
        protected void ShowAlertMessage(Message msg)
        {
            TempData["Message"] = "<script>alert('" + HttpUtility.HtmlDecode(msg.MsgText) + "');</script>";
        }
        //Tuan them
        protected void ShowAlertPopup(Message msg)
        {
            TempData["Message"] = "<script>$(document).ready(function () { JS_LIB.msgBox('" + HttpUtility.HtmlDecode(msg.MsgText) + "', 300);});</script>";
        }
        #endregion

        public class FileUploadJsonResult : JsonResult
        {
            public override void ExecuteResult(ControllerContext context)
            {
                this.ContentType = "text/html";
                context.HttpContext.Response.Write("<textarea>");
                base.ExecuteResult(context);
                context.HttpContext.Response.Write("</textarea>");
            }
        }

    }
}
